package tech.aistar.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tech.aistar.constants.MessageConstant;
import tech.aistar.pojo.Flower;
import tech.aistar.pojo.Order;
import tech.aistar.pojo.ShoppingCart;
import tech.aistar.pojo.dto.CartPayDTO;
import tech.aistar.pojo.result.ResultReturn;
import tech.aistar.service.CartService;
import tech.aistar.service.FlowerService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cart")
@Slf4j
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private FlowerService flowerService;


    @GetMapping()
    public ShoppingCart findByUserId(HttpServletRequest request){
        Integer userId = (Integer)request.getAttribute("userId");
        List<Integer> ids = cartService.getFlowerIdsByUserId(userId);
        List<Integer> counts = cartService.getFlowerCountsByUserId(userId);
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        List<Flower> flowers = new ArrayList<>();
        List<Integer> cs = new ArrayList<>();
        ids.forEach(id -> {
            flowers.add(flowerService.findById(id));
        });
        counts.forEach(count -> {
            cs.add(count);
        });
        shoppingCart.setFlowers(flowers);
        shoppingCart.setCount(cs);

        return shoppingCart;
    }

    @GetMapping("/getcart/{user_id}/{flower_id}")
    public Integer findByUserIdAndFlowerId(@PathVariable("user_id") Integer user_id,@PathVariable("flower_id") Integer flower_id){
        Integer shoppingCart = cartService.fingByUserIdAndFlowerId(user_id,flower_id);
        return shoppingCart;
    }

    @DeleteMapping("/deleteFlower/{user_id}/{flower_id}")
    public Map deleteFlower(@PathVariable("user_id") Integer user_id, @PathVariable("flower_id") Integer flower_id){
        int i = cartService.deleteByUserIdAndFlowerId(user_id, flower_id);
        if (i==0){
            System.out.println("删除失败");
        }else {
            System.out.println("删除成功");
        }
        return cartService.findByUserId(user_id);
    }

    @PutMapping("/updateCount/{user_id}/{flower_id}/{count}")
    public Map updateCount(@PathVariable("user_id") Integer user_id,@PathVariable("flower_id") Integer flower_id,@PathVariable("count") Integer count){
        int i = cartService.updateCount(user_id, flower_id, count);
        if (i==0){
            System.out.println("修改失败");
        }else {
            System.out.println("修改成功");
        }
        return cartService.findByUserId(user_id);
    }

    @PostMapping("/addFlower/{user_id}/{flower_id}/{count}")
    public ResultReturn addFlower(@PathVariable("user_id") Integer user_id, @PathVariable("flower_id") Integer flower_id, @PathVariable("count") Integer count){
        System.out.println(user_id+"-----"+flower_id+"-----"+count);
        int i = cartService.insert(user_id, flower_id, count);
        if (i==0){
            System.out.println("添加失败");
            return ResultReturn.fail(MessageConstant.FAIL);
        }else {
            System.out.println("添加成功");
            return ResultReturn.success();
        }
    }

    @DeleteMapping("/deleteFlowers/{user_id}/{flowerids}")
    public Map deleteFlowers(@PathVariable("user_id") Integer user_id, @PathVariable("flowerids")List<Integer> flowerids){;
        String m = "(";
        for (int i = 0; i < flowerids.size(); i++) {
            m += flowerids.get(i)+",";
        }
        m = m.substring(0,m.length()-1)+")";
        int i = cartService.deleteByUserIdAndFlowerIds(user_id,m);
        if (i==0){
            System.out.println("删除失败");
        }else {
            System.out.println("删除成功");
        }

        return cartService.findByUserId(user_id);
    }

    /**
     * 购物车支付
     * @param cartPayDTO
     * @return
     */
    @PostMapping("/pay")
    public ResultReturn pay(HttpServletRequest request,@RequestBody CartPayDTO cartPayDTO){
        cartPayDTO.setUserId((Integer)request.getAttribute("userId"));
        log.info("购物车支付：{}",cartPayDTO);
        return cartService.pay(cartPayDTO);
    }


}
